package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AccountLogHisService;
import com.qdlc.p2p.common.constant.LogTplNid;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.AccountLogHisDao;
import com.qdlc.p2p.dal.dao.DictDao;
import com.qdlc.p2p.dal.dto.AccountLogHis;
import com.qdlc.p2p.dal.dto.Dict;
import com.qdlc.p2p.dal.model.AccountLogHisModel;

/**
 * 资金记录历史
 *
 * @author wuaj
 * @version 1.1.0.0
 * @since 2016年3月17日
 */
@Service("accountLogHisService")
public class AccountLogHisServiceImpl implements AccountLogHisService {

    @Resource
    private AccountLogHisDao accountLogHisDao;

    @Resource
    private DictDao dictDao;

    @Override
    public void add(AccountLogHis log) {
        accountLogHisDao.save(log);
    }

    @Override
    public PageDataList<AccountLogHisModel> list(AccountLogHisModel model) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("user.userId", model.getUser().getUserId());
        if (StringUtil.isNotBlank(model.getStartTime())) {
            Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
            param.addParam("addTime", Operators.GTE, start);
        }
        Date nowdate = DateUtil.getDate(System.currentTimeMillis() / 1000 + "");
        if (model.getTime() == 7) {
            param.addParam("addTime", Operators.GTE, DateUtil.rollDay(nowdate, -7));
            param.addParam("addTime", Operators.LTE, nowdate);
        } else if (model.getTime() > 0 && model.getTime() < 4) {
            param.addParam("addTime", Operators.GTE, DateUtil.rollMon(nowdate, -model.getTime()));
            param.addParam("addTime", Operators.LTE, nowdate);
        }
        if (StringUtil.isNotBlank(model.getEndTime())) {
            Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
            param.addParam("addTime", Operators.LTE, end);
        }
        if (StringUtil.isNotBlank(model.getStatus()) && !("0").equals(model.getStatus()) && !"undefined".equals(model.getStatus())) {
            if (LogTplNid.AL_ONLINE_RECHARGE.equals(model.getStatus())) {
                SearchFilter filter1 = new SearchFilter("type", LogTplNid.AL_OFF_RECHARGE);
                SearchFilter filter2 = new SearchFilter("type", LogTplNid.AL_ONLINE_RECHARGE);
                SearchFilter filter3 = new SearchFilter("type", LogTplNid.AL_SYSTEM_RECHARGE);
                param.addOrFilter(filter1, filter2, filter3);
            } else {
                param.addParam("type", model.getStatus());
            }
        }
        param.addOrder(OrderType.DESC, "id");
        param.addPage(model.getPage());

        PageDataList<AccountLogHis> pageDateList = accountLogHisDao.findPageList(param);
        PageDataList<AccountLogHisModel> pageDateList_ = new PageDataList<AccountLogHisModel>();
        List<AccountLogHisModel> list = new ArrayList<AccountLogHisModel>();
        pageDateList_.setPage(pageDateList.getPage());
        if (pageDateList.getList().size() > 0) {
            for (int i = 0; i < pageDateList.getList().size(); i++) {
                AccountLogHis c = pageDateList.getList().get(i);
                AccountLogHisModel cm = AccountLogHisModel.instance(c);
                Dict d = dictDao.find("account_type", c.getType());
                if (d != null) {
                    cm.setTypeName(d.getName());
                }
                if (c.getToUser() != null) {
                    cm.setToUser(c.getToUser());
                    cm.setToUserName(c.getToUser().getUserName());
                }
                list.add(cm);
            }
        }
        pageDateList_.setList(list);
        return pageDateList_;
    }

    @Override
    public PageDataList<AccountLogHisModel> accountLogList(AccountLogHisModel model, int pageNumber, int pageSize) {
        return accountLogHisDao.accountLogList(model, pageNumber, pageSize);
    }

    @Override
    public double todayEarnAmount(long userId) {
        return accountLogHisDao.todayEarnAmount(userId);
    }

    @Override
    public double earnAmount(long userId) {
        return accountLogHisDao.earnAmount(userId);
    }

    @Override
    public double earnAmount() {
        return accountLogHisDao.earnAmount();
    }

    @Override
    public double[] amount(String type, String year) {
        double[] amounts = new double[12];
        String date;
        for (int i = 0; i < 12; i++) {
            date = year + (i > 8 ? "-" : "-0") + (i + 1);
            amounts[i] = accountLogHisDao.amount(type, date);
        }
        return amounts;
    }

    @Override
    public double[] amount(String[] earn_type, String pay_type, String year) {
        double[] amounts = new double[12];
        String date;
        for (int i = 0; i < 12; i++) {
            date = year + (i > 8 ? "-" : "-0") + (i + 1);
            amounts[i] = BigDecimalUtil.sub(accountLogHisDao.amount(earn_type, date), accountLogHisDao.amount(pay_type, date));
        }
        return amounts;
    }
}
